package com.bsy.common.base

import android.os.Bundle
import android.view.View
import androidx.annotation.CallSuper
import androidx.databinding.ViewDataBinding
import com.bsy.common.constant.PAGE_SIZE
import com.bsy.framework.base.BaseMvvmFragment
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

/**
 * Created by Sincerity on 2023/10/20.
 * Copyright
 * 描述：
 */
abstract class BaseRefreshFragment<DB : ViewDataBinding, VM : BaseRefreshViewModel<T>, T> : BaseMvvmFragment<DB, VM>(), OnRefreshLoadMoreListener {
    private var mWrapRefresh: WrapRefresh? = null

    protected abstract fun onBindWrapRefresh(): WrapRefresh
    protected abstract fun refresh(refreshLayout: RefreshLayout)
    protected abstract fun loadMore(refreshLayout: RefreshLayout)


    @CallSuper
    override fun initListener() {
        super.initListener()
        mWrapRefresh = onBindWrapRefresh()
        mWrapRefresh?.refreshLayout?.setOnRefreshLoadMoreListener(this)
    }

    override fun initView(view: View, savedInstanceState: Bundle?) {
        mViewModel.getFinishRefreshEvent().observe(this) {
            if (it == null) {
                // 处理刷新失败的情况
                handleRefreshFailure()
                return@observe
            }
            if (it.isEmpty()) {
                // 处理刷新成功但没有数据的情况
                handleRefreshSuccessWithNoData()
                return@observe
            }
            // 处理刷新成功有数据的情况
            handleRefreshSuccessWithData(it)
        }
        mViewModel.getFinishLoadMoreEvent().observe(this) {
            if (it == null) {
                // 处理加载更多失败的情况
                handleLoadMoreFailure()
                return@observe
            }
            if (it.isEmpty()) {
                // 处理加载更多成功但没有更多数据的情况
                handleLoadMoreSuccessWithNoData()
                return@observe
            }
            // 处理加载更多成功有数据的情况
            handleLoadMoreSuccessWithData(it)
        }
    }

    /**
     * 下拉刷新失败
     *
     */
    protected open fun handleRefreshFailure() {
        mWrapRefresh?.refreshLayout?.finishRefresh(false)
        onRefreshSuccess(emptyList())
    }

    /**
     * 上拉加载失败
     *
     */
    protected open fun handleLoadMoreFailure() {
        mWrapRefresh?.refreshLayout?.finishLoadMore(false)
    }

    /**
     * 下拉刷新没有数据
     *
     */
    protected open fun handleRefreshSuccessWithNoData() {
        mWrapRefresh?.refreshLayout?.finishRefresh(true)
        onRefreshSuccess(emptyList())
    }

    /**
     * 上拉加载没有数据
     *
     */
    protected open fun handleLoadMoreSuccessWithNoData() {
        mWrapRefresh?.refreshLayout?.finishLoadMoreWithNoMoreData()
        mWrapRefresh?.refreshLayout?.setEnableLoadMore(true)
        onLoadMoreSuccess(emptyList())
    }

    /**
     * 下拉刷新 有数据
     *
     * @param list 需要刷新的数据
     */
    protected open fun handleRefreshSuccessWithData(list: List<T>) {
        if (list.size < PAGE_SIZE) {
            mWrapRefresh?.refreshLayout?.setNoMoreData(true)
            mWrapRefresh?.refreshLayout?.setEnableLoadMore(false)
        } else {
            mWrapRefresh?.refreshLayout?.setEnableLoadMore(true)
        }
        mWrapRefresh?.refreshLayout?.resetNoMoreData()
        mWrapRefresh?.refreshLayout?.setEnableRefresh(true)
        mWrapRefresh?.refreshLayout?.finishRefresh(true)
        onRefreshSuccess(list)
    }

    /**
     * 上拉加成功
     *
     * @param list 需要加载的数据
     */
    protected open fun handleLoadMoreSuccessWithData(list: List<T>?) {
        mWrapRefresh?.refreshLayout?.finishLoadMore(true)
        mWrapRefresh?.refreshLayout?.setEnableLoadMore(true)
        onLoadMoreSuccess(list)
    }

    /**
     * 刷新成功
     *
     * @param list 刷新的数据
     */
    protected open fun onRefreshSuccess(list: List<T>?) {
        if (mWrapRefresh?.quickAdapter != null) {
            mWrapRefresh?.quickAdapter?.setList(list)
        }
    }

    /**
     * 加载成功
     *
     * @param list
     */
    protected open fun onLoadMoreSuccess(list: List<T>?) {
        if (mWrapRefresh?.quickAdapter != null && list != null) {
            mWrapRefresh?.quickAdapter?.addData(list)
        }
    }

    final override fun onLoadMore(refreshLayout: RefreshLayout) {
        loadMore(refreshLayout)
    }

    final override fun onRefresh(refreshLayout: RefreshLayout) {
        refresh(refreshLayout)
    }

    override fun onDestroy() {
        if (null != mWrapRefresh) mWrapRefresh?.refreshLayout?.setOnRefreshLoadMoreListener(null)
        super.onDestroy()
    }

    inner class WrapRefresh(var refreshLayout: SmartRefreshLayout?, quickAdapter: BaseQuickAdapter<T?, BaseViewHolder>) {

        var quickAdapter: BaseQuickAdapter<T?, BaseViewHolder>

        init {
            this.quickAdapter = quickAdapter
        }
    }
}